package com.laiketui.admin.mch.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laiketui.admin.mch.api.MchService;
import com.laiketui.common.api.PublicMchService;
import com.laiketui.common.api.PubliceService;
import com.laiketui.common.mapper.*;
import com.laiketui.common.utils.tool.ImgUploadUtils;
import com.laiketui.common.utils.tool.cache.RedisDataTool;
import com.laiketui.common.utils.tool.data.DataCheckTool;
import com.laiketui.common.utils.tool.str.StringUtils;
import com.laiketui.core.cache.RedisUtil;
import com.laiketui.core.common.SplitUtils;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.core.utils.tool.DataUtils;
import com.laiketui.core.utils.tool.DateUtil;
import com.laiketui.domain.config.AdminCgModel;
import com.laiketui.domain.config.BannerModel;
import com.laiketui.domain.config.PrintSetupModel;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.domain.lktconst.ErrorCode;
import com.laiketui.domain.log.AdminRecordModel;
import com.laiketui.domain.mch.MchClassModel;
import com.laiketui.domain.mch.MchConfigModel;
import com.laiketui.domain.mch.MchModel;
import com.laiketui.domain.user.User;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.domain.vo.mch.AddMchVo;
import com.laiketui.domain.vo.mch.MchPrintSetupVo;
import com.laiketui.domain.vo.pc.AddBannerInfoVo;
import com.laiketui.root.license.CryptoUtil;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 店铺设置
 *
 * @author Trick
 * @date 2021/5/27 11:37
 */
@Service
public class MchServiceImpl implements MchService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MchModelMapper mchModelMapper;

    @Autowired
    private PubliceService publiceService;

    @Autowired
    private AdminCgModelMapper adminCgModelMapper;

    @Autowired
    private BannerModelMapper bannerModelMapper;

    @Autowired
    private PublicMchService publicMchService;

    @Autowired
    private UserBaseMapper userBaseMapper;

    @Autowired
    private MchClassModelMapper mchClassModelMapper;

    @Override
    public Map<String, Object> index(MainVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            Map<String, Object> mchMap = new HashMap<>(16);

            MchModel mchModel = new MchModel();
            mchModel.setStore_id(vo.getStoreId());
            mchModel.setId(user.getMchId());
            mchModel = mchModelMapper.selectOne(mchModel);
            if (mchModel != null && StringUtils.isNotEmpty(mchModel.getSheng()) && StringUtils.isNotEmpty(mchModel.getShi()) && StringUtils.isNotEmpty(mchModel.getXian())) {
                mchModel.setHead_img(publiceService.getImgPath(mchModel.getHead_img(), vo.getStoreId()));
                mchModel.setPoster_img(publiceService.getImgPath(mchModel.getPoster_img(), vo.getStoreId()));
                mchMap = JSON.parseObject(JSON.toJSONString(mchModel), new TypeReference<Map<String, Object>>() {
                });
                mchMap.put("logo", publiceService.getImgPath(mchModel.getLogo(), vo.getStoreId()));
                Integer cid = MapUtils.getInteger(mchMap, "cid");
                MchClassModel mchClassModel = mchClassModelMapper.selectByPrimaryKey(cid);
                if (!Objects.isNull(mchClassModel)) {
                    mchMap.put("className", mchClassModel.getName());
                }
                //营业状态
                String is_open = MapUtils.getString(mchMap, "is_open");
                //未营业
                if (is_open.equals("0") || is_open.equals("2")){
                    mchMap.put("is_open", is_open);
                }else if (is_open.equals("1") ){
                    //营业时间判断是否营业
                    String[] businessHours = MapUtils.getString(mchMap, "business_hours").split(SplitUtils.BL);
                    //开始时间
                    Date startTime = com.laiketui.common.utils.tool.DateUtil.dateFormateToDate(businessHours[0], GloabConst.TimePattern.HM);
                    //结束时间
                    Date endTime = com.laiketui.common.utils.tool.DateUtil.dateFormateToDate(businessHours[1], GloabConst.TimePattern.HM);
                    //当前时间
                    Date currentDate = com.laiketui.common.utils.tool.DateUtil.dateFormateToDate(new Date(), GloabConst.TimePattern.HM);
                    //开始时间大于结束时间(跨天：例如 19：00~04：00 则当前时间 >= 19:00 || 当前时间 <= 04:00 -> 营业 )
                    if (com.laiketui.common.utils.tool.DateUtil.dateCompare(startTime, endTime)){
                        if (!com.laiketui.common.utils.tool.DateUtil.dateCompare(startTime, currentDate)
                                || !com.laiketui.common.utils.tool.DateUtil.dateCompare(currentDate, endTime)){
                            mchMap.put("is_open", "1");
                        }else {
                            //未营业
                            mchMap.put("is_open", "2");
                        }
                    }else {//开始时间小于结束时间（当天）则当前时间 >= 19:00 && 当前时间 <= 04:00 -> 营业
                        if (!com.laiketui.common.utils.tool.DateUtil.dateCompare(startTime, currentDate)
                                && !com.laiketui.common.utils.tool.DateUtil.dateCompare(currentDate, endTime)){
                            mchMap.put("is_open", "1");
                        }else {
                            mchMap.put("is_open", "2");
                        }
                    }
                }else {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "店铺未设置营业状态");
                }

                AdminCgModel adminCgModel = new AdminCgModel();
                adminCgModel.setG_ParentID(0);
                adminCgModel.setG_CName(mchModel.getSheng());
                AdminCgModel adminCg = adminCgModelMapper.selectOne(adminCgModel);
                if (adminCg != null) {
                    mchMap.put("sheng_id", adminCg.getGroupID());
                    adminCgModel.setG_ParentID(adminCg.getGroupID());
                    adminCgModel.setG_CName(mchModel.getShi());
                    adminCg = adminCgModelMapper.selectOne(adminCgModel);
                    if (adminCg != null) {
                        mchMap.put("shi_id", adminCg.getGroupID());
                        adminCgModel.setG_ParentID(adminCg.getGroupID());
                        adminCgModel.setG_CName(mchModel.getXian());
                        adminCg = adminCgModelMapper.selectOne(adminCgModel);
                        if (adminCg != null) {
                            mchMap.put("xian_id", adminCg.getGroupID());
                        }
                    }
                }
            }

            resultMap.put("res", mchMap);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("店铺信息 异常  ", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "index");
        }
        return resultMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> setPassword(MainVo vo, String pwd, String pwdOld) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            MchModel mchModel = publicMchService.verificationMchExis(vo.getStoreId(), user.getUser_id(), user.getMchId());
            user = userBaseMapper.selectByPrimaryKey(user.getId());
            if (StringUtils.isEmpty(pwd)) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_XMMBNWK, "新密码不能为空");
            }
            if (StringUtils.isEmpty(pwdOld)) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_YMMBNWK, "原密码不能为空");
            }
            if (!pwdOld.equals(CryptoUtil.strDecode(user.getMima()))) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_YMMBZQ, "原密码不正确");
            }
            //修改当前用户登录密码
            User userUpdate = new User();
            userUpdate.setId(user.getId());
            userUpdate.setMima(CryptoUtil.strEncode(pwd));
            int row = userBaseMapper.updateByPrimaryKeySelective(userUpdate);
            if (row < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
            publiceService.addAdminRecord(vo.getStoreId(), "修改了密码",AdminRecordModel.Type.UPDATE, vo.getAccessId());
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("设置店铺密码 异常  ", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "setPassword");
        }
        return resultMap;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> edit(AddMchVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            MchModel mchOld = new MchModel();
            mchOld.setStore_id(vo.getStoreId());
            mchOld.setId(user.getMchId());
            mchOld = mchModelMapper.selectOne(mchOld);
            if (mchOld == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_DPBCZ, "店铺不存在");
            }
            if (StringUtils.isEmpty(vo.getMchName())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_DPMCBNWK, "公司名称不能为空");
            }
            if (StringUtils.isEmpty(vo.getTel())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LXDHBNWK, "联系电话不能为空");
            }
//            if (StringUtils.isEmpty(vo.getConfines())) {
//                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_JYFWBNWK, "经营范围不能为空");
//            }
            if (StringUtils.isEmpty(vo.getAddress())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LXDZBNWK, "联系地址不能为空");
            }
//            if (StringUtils.isEmpty(vo.getLogo())) {
//                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_QXZ, "请选择logo");
//            }
//            if (Objects.isNull(vo.getCid())) {
//                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "请选择分类");
//            }
//            if (StringUtils.isEmpty(vo.getPosterImg())) {
//                throw new LaiKeAPIException(ErrorCode.BizErrorCode.PARAMATER_ERROR, "宣传图不能为空");
//            }
//            if (StringUtils.isEmpty(vo.getHeadImg())) {
//                throw new LaiKeAPIException(ErrorCode.BizErrorCode.PARAMATER_ERROR, "头像不能为空");
//            }
            MchModel mchModelUpdate = new MchModel();
//            if(StringUtils.isEmpty(vo.getIsOpen())){
//                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "请输入营业状态");
//            }
//            mchModelUpdate.setIs_open(vo.getIsOpen().toString());
            //营业中-有营业时间
//            if (vo.getIsOpen().equals(1)){
//                if (StringUtils.isEmpty(vo.getBusinessHours())){
//                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "请输入营业时间");
//                }
//                //营业时间判断是否营业
//                String[] businessHours = vo.getBusinessHours().split(SplitUtils.BL);
//                //开始时间
//                Date startTime = com.laiketui.common.utils.tool.DateUtil.dateFormateToDate(businessHours[0], GloabConst.TimePattern.HM);
//                //结束时间
//                Date endTime = com.laiketui.common.utils.tool.DateUtil.dateFormateToDate(businessHours[1], GloabConst.TimePattern.HM);
//                if (Objects.equals(startTime, endTime)){
//                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "营业时间开始时间不能等于结束时间");
//                }
//                mchModelUpdate.setBusiness_hours(vo.getBusinessHours());
//            }
            mchModelUpdate.setId(mchOld.getId());
            mchModelUpdate.setTel(vo.getTel());
            mchModelUpdate.setShi(vo.getShi());
            mchModelUpdate.setXian(vo.getXian());
            mchModelUpdate.setSheng(vo.getShen());
            mchModelUpdate.setAddress(vo.getAddress());
            mchModelUpdate.setLegalPersonIdCard(vo.getLegalPersonIdCard());
            mchModelUpdate.setLegalPersonName(vo.getLegalPersonName());
//            mchModelUpdate.setShop_range(vo.getConfines());
//            mchModelUpdate.setShop_information(vo.getMchInfo());
//            mchModelUpdate.setLogo(ImgUploadUtils.getUrlImgByName(vo.getLogo(), true));
//            mchModelUpdate.setPoster_img(ImgUploadUtils.getUrlImgByName(vo.getPosterImg(), true));
//            mchModelUpdate.setHead_img(ImgUploadUtils.getUrlImgByName(vo.getHeadImg(), true));
            mchModelUpdate.setName(vo.getMchName());
//            mchModelUpdate.setIs_invoice(vo.getIsInvoice());
//            mchModelUpdate.setCid(vo.getCid());
            //校验店铺名称
            if (!mchOld.getName().equals(vo.getMchName())) {
                MchModel mchModel = new MchModel();
                mchModel.setStore_id(vo.getStoreId());
                mchModel.setName(vo.getMchName());
                mchModel.setRecovery(DictionaryConst.ProductRecycle.NOT_STATUS);
                int count = mchModelMapper.selectCount(mchModel);
                if (count > 0) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_DPMCYCZ, "店铺名称已存在");
                }
            }

//            if (vo.getRoomid() != null && vo.getRoomid() > 0) {
//                mchModelUpdate.setRoomid(vo.getRoomid());
//                mchModelUpdate.setOld_roomid(mchOld.getRoomid());
//            }
//            if (!StringUtils.isEmpty(vo.getNature())) {
//                mchModelUpdate.setShop_nature(vo.getNature());
//            }
            DataCheckTool.checkMchDataFormate(mchModelUpdate);
//            mchModelUpdate.setShop_range(vo.getConfines());
            int count = mchModelMapper.updateByPrimaryKeySelective(mchModelUpdate);
            if (count < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_XGSB, "修改失败");
            }
            publiceService.addAdminRecord(vo.getStoreId(),"修改了店铺设置信息",AdminRecordModel.Type.UPDATE,vo.getAccessId());
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("编辑店铺 异常 ", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "edit");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> bannerList(MainVo vo, Integer id) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            if (id != null) {
                parmaMap.put("id", id);
            }
            parmaMap.put("mch_id", user.getMchId());
            parmaMap.put("review_status", DictionaryConst.MchExameStatus.EXAME_PASS_STATUS);
            parmaMap.put("sort_sort", DataUtils.Sort.DESC.toString());
            parmaMap.put("pageStart", vo.getPageNo());
            parmaMap.put("pageEnd", vo.getPageSize());

            List<Map<String, Object>> dataList = bannerModelMapper.selectDynamic(parmaMap);
            for (Map<String, Object> map : dataList) {
                String imagUrl = String.valueOf(map.get("image"));
                map.put("image", publiceService.getImgPath(imagUrl, vo.getStoreId()));
            }
            int total = bannerModelMapper.countDynamic(parmaMap);

            resultMap.put("list", dataList);
            resultMap.put("total", total);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取轮播图列表 异常 ", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "bannerList");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> bannerPathList(MainVo vo, Integer type) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            parmaMap.put("mchId", user.getMchId());
            parmaMap.put("type0", type);
            parmaMap.put("status", 1);
            parmaMap.put("type", 1);
            parmaMap.put("add_date_sort", DataUtils.Sort.DESC.toString());
            List<Map<String, Object>> list = jumpPathModelMapper.selectDynamic(parmaMap);

            resultMap.put("list", list);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("轮播图路径分类 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "bannerPathList");
        }
        return resultMap;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBannerInfo(AddBannerInfoVo vo, Integer type) throws LaiKeAPIException {
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            int count;
            if (StringUtils.isEmpty(vo.getImageUrl())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTBNWK, "轮播图不能为空");
            }
            if (StringUtils.isEmpty(vo.getPath())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTTZLJBNWK, "轮播图跳转路径不能为空");
            }
            BannerModel bannerModelOld = null;
            if (vo.getId() != null && vo.getId() > 0) {
                bannerModelOld = bannerModelMapper.selectByPrimaryKey(vo.getId());
            }

            BannerModel bannerModelSave = new BannerModel();
            // &shop_id 设置指定店铺查询商品
            if (type == 1){
                vo.setPath(vo.getPath()+"&shop_id="+user.getMchId());
            }
            bannerModelSave.setUrl(vo.getPath());
            bannerModelSave.setImage(ImgUploadUtils.getUrlImgByName(vo.getImageUrl(), true));
            bannerModelSave.setType(String.valueOf(type));
            if (bannerModelOld != null) {
                bannerModelSave.setId(bannerModelOld.getId());
                count = bannerModelMapper.updateByPrimaryKeySelective(bannerModelSave);

                publiceService.addAdminRecord(vo.getStoreId(),"修改了轮播图ID："+vo.getId(),AdminRecordModel.Type.UPDATE, vo.getAccessId());
            } else {
                bannerModelSave.setSort(bannerModelMapper.getMaxSort(vo.getStoreId()));
                bannerModelSave.setMch_id(user.getMchId());
                bannerModelSave.setStore_id(vo.getStoreId());
                bannerModelSave.setAdd_date(new Date());
                count = bannerModelMapper.insertSelective(bannerModelSave);

                publiceService.addAdminRecord(vo.getStoreId(),"添加了轮播图ID："+bannerModelSave.getId(),AdminRecordModel.Type.ADD, vo.getAccessId());
            }

            if (count < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("添加/编辑轮播图 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "addBannerInfo");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setBannerSort(MainVo vo, Integer id, Integer sort) throws LaiKeAPIException {
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            int row;
            if (id == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTBNWK, "轮播图id不能为空");
            }
            if (sort == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTXHBNWK, "轮播图序号不能为空");
            }
            BannerModel bannerModelOld = bannerModelMapper.selectByPrimaryKey(id);
            if (bannerModelOld == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTBCZ, "轮播图不存在");
            }
            BannerModel bannerUpdate = new BannerModel();
            bannerUpdate.setId(id);
            bannerUpdate.setSort(sort);
            row = bannerModelMapper.updateByPrimaryKeySelective(bannerUpdate);
            if (row < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("添加/编辑轮播图 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "addBannerInfo");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBannerById(MainVo vo,int id) throws LaiKeAPIException {
        try {
            BannerModel bannerModel = bannerModelMapper.selectByPrimaryKey(id);
            if (bannerModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTBCZ, "轮播图不存在");
            }
            if (bannerModelMapper.deleteByPrimaryKey(id) < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
            publiceService.addAdminRecord(vo.getStoreId(),"删除了轮播图ID："+id,AdminRecordModel.Type.DEL, vo.getAccessId());
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("删除轮播图 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "delBannerById");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void topBannerById(int id) throws LaiKeAPIException {
        try {
            BannerModel bannerModel = bannerModelMapper.selectByPrimaryKey(id);
            if (bannerModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_LBTBCZ, "轮播图不存在");
            }
            BannerModel bannerModelUpdate = new BannerModel();
            bannerModelUpdate.setId(id);
            bannerModelUpdate.setSort(bannerModelMapper.getMaxSort(bannerModel.getStore_id()));

            if (bannerModelMapper.updateByPrimaryKeySelective(bannerModelUpdate) < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("置顶轮播图 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "topBannerById");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delMchInfo(MainVo vo) throws LaiKeAPIException {
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            //注销店铺
            return publicMchService.cancellationShop(vo.getStoreId(), user.getMchId());
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("注销 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "delMchInfo");
        }
    }

    @Override
    public Map<String, Object> getMchConfig(MainVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            MchConfigModel mchConfigModel = publicMchService.getMchConfig(vo.getStoreId(), user.getMchId());
            String[] uploadGoodsStatus = new String[]{};
            if (mchConfigModel != null) {
                uploadGoodsStatus = mchConfigModel.getCommodity_setup().split(SplitUtils.DH);
            }
            resultMap.put("uploadGoodsStatus", uploadGoodsStatus);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("注销 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "delMchInfo");
        }

        return resultMap;
    }

    @Override
    public Map<String, Object> mchClassList(MainVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil);
            Map<String, Object> paramMap = new HashMap<>(16);
            paramMap.put("storeId", vo.getStoreId());
            paramMap.put("sort_sort", DataUtils.Sort.DESC.toString());
            paramMap.put("pageStart", vo.getPageNo());
            paramMap.put("pageEnd", vo.getPageSize());
            int i = mchClassModelMapper.countCondition(paramMap);
            List<Map<String, Object>> list = new ArrayList<>();
            if (i > 0) {
                list = mchClassModelMapper.selectCondition(paramMap);
                list.stream().forEach(map -> {
                    map.put("img", publiceService.getImgPath(MapUtils.getString(map, "img"), vo.getStoreId()));
                    map.put("add_date", DateUtil.dateFormate(MapUtils.getString(map, "add_date"), com.laiketui.core.lktconst.GloabConst.TimePattern.YMDHMS));
                });
            }
            resultMap.put("total", i);
            resultMap.put("list", list);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取店铺分类列表 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "mchClassList");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getMchPrintSetup(MainVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            //订单打印配置
            PrintSetupModel printSetupModel = new PrintSetupModel();
            printSetupModel.setStoreId(vo.getStoreId());
            printSetupModel.setMchId(user.getMchId());
            printSetupModel = printSetupModelMapper.selectOne(printSetupModel);
            if (StringUtils.isEmpty(printSetupModel)){
                printSetupModel = new PrintSetupModel();
            }
            resultMap.put("printSetupConfig", printSetupModel);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取订单打印配置 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getMchPrintSetup");
        }
        return resultMap;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setMchPrintSetup(MchPrintSetupVo vo) throws LaiKeAPIException {
        try {
            User user = RedisDataTool.getRedisUserCache(vo.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
            int count;
            //订单打印配置
            PrintSetupModel printSetupModel = new PrintSetupModel();
            printSetupModel.setStoreId(vo.getStoreId());
            printSetupModel.setMchId(user.getMchId());
            printSetupModel = printSetupModelMapper.selectOne(printSetupModel);
            if (printSetupModel != null){
                printSetupModel.setSheng(vo.getSheng());
                printSetupModel.setShi(vo.getShi());
                printSetupModel.setXian(vo.getXian());
                printSetupModel.setAddress(vo.getAddress());
                printSetupModel.setPrintName(vo.getPrintName());
                printSetupModel.setPrintUrl(vo.getPrintUrl());
                printSetupModel.setPhone(vo.getPhone());
                count = printSetupModelMapper.updateByPrimaryKeySelective(printSetupModel);
            }else {
                printSetupModel = new PrintSetupModel();
                printSetupModel.setStoreId(vo.getStoreId());
                //管理后台店铺id为0
                printSetupModel.setMchId(user.getMchId());
                printSetupModel.setSheng(vo.getSheng());
                printSetupModel.setShi(vo.getShi());
                printSetupModel.setXian(vo.getXian());
                printSetupModel.setAddress(vo.getAddress());
                printSetupModel.setPrintName(vo.getPrintName());
                printSetupModel.setPrintUrl(vo.getPrintUrl());
                printSetupModel.setPhone(vo.getPhone());
                printSetupModel.setAddTime(new Date());
                count = printSetupModelMapper.insert(printSetupModel);
            }
            publiceService.addAdminRecord(vo.getStoreId(), "修改了打印配置信息",AdminRecordModel.Type.UPDATE,vo.getAccessId());
            if (count < 1){
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("上传订单打印配置 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getMchPrintSetup");
        }
    }

    @Autowired
    private JumpPathModelMapper jumpPathModelMapper;

    @Autowired
    private MchConfigModelMapper mchConfigModelMapper;

    @Autowired
    private PrintSetupModelMapper printSetupModelMapper;
}

